 <?php
include('connection/connection.php');
  $distId=$_REQUEST['distId'];
 $file1=$_REQUEST['fl'];
 $file = "$file1"._."$distId.csv";
  $temp_str = "";
  if($file1=='tbl_dtls_q3_loc')
  {
  $querystring="select * from ".$file1." where district_id=".$distId."";
  }
  else
  {
  $querystring="SELECT * FROM ".$file1." where loc_id in(select id from tbl_dtls_q3_loc where district_id=".$distId.")";
   }
  $result_csv=mysql_query($querystring);
	$fh=fopen($file,"w+") or die ("unable to open file");
//$row=mysql_fetch_assoc($result_csv);
  while($row=mysql_fetch_assoc($result_csv)):
      foreach ($row as $val):
         $val = str_replace('"', "'", $val);
      endforeach;
      $t_temp_str =  implode(',',$row)."\n";
	  $temp_str .= $t_temp_str;
      fwrite($fh, $t_temp_str, strlen($t_temp_str));
  endwhile;
  fclose($fh);
   if ($temp_str > "")
     {
       header("Cache-Control: no-cache, must-revalidate");
	   header("Content-Type: application/force-download");
	   //header("Content-Type: application/vnd.ms-excel");
	   header("Content-disposition: attachment; filename=$file");
       header("Content-Transfer-Encoding: binary");
       header("Content-Length: ".strlen($temp_str));
       header("Expires: 0");
	   header("Cache-control: private");
	  echo $temp_str;
     }
?>